<?php
	class Log {
		static private $target;
		static private $disabled;
		static private $format;
		static private $cfg;
		
		public static function target($logName, $format = false) {
			self::$cfg = new Config('xten');
			if (!(
				self::$cfg->log_write_errors || 
				self::$cfg->log_write_warnings || 
				self::$cfg->log_write_logs				
			)) return;
			self::$format = ($format) ? $format : self::$cfg->log_format; 
			if (self::$target) {
				fclose(self::$target);
			}
			
			$fname = self::$cfg->log_directory.'/'.$logName;
			if ((@filesize($fname) / 1024 > self::$cfg->log_max_size)) unlink($fname);
			if(self::$disabled = self::$cfg->log_disabled) return;
			self::$target = fopen($fname, "a");
			if (self::$target === false) throw new Exception("Unable to open logfile: $logName");
		}
		
		public static function fatal($message) {
			$message = self::write('ERROR', $message);
			if (self::$cfg->display_errors) throw new Exception(htmlentities($message));
		}
		
		public static function put($message) {
			$message = self::write('LOG', $message);
			if (self::$cfg->display_logs) echo '<br/>'.htmlentities($message).'<br/>';
		}
		
		public static function warning($message) {
			$message = self::write('WARNING', $message);
			if (self::$cfg->display_warnings) echo '<br/>'.htmlentities($message).'<br/>';
		}
		
		private static function write($level, $message) {
			switch ($level) {
				case 'LOG': $p = '>'; break;
				case 'WARNING': $p = '!'; break;
				case 'ERROR': $p = 'X'; break;
			}
			$optName = 'log_write_'.strtolower($level).'s';
			$message = $p.' '.self::format($level, $message);
			if (!self::$cfg->$optName) return $message; 
			if (self::$disabled) return $message;
			if (!self::$target) {
				self::target('system.log');
				self::fatal('Trying to put message before log opened!');
				throw new Exception('Trying to put message before log opened!');
			}
			fputs(self::$target, $message."\r\n");
			return $message;
		}
		
		private function format($level, $message) {
			$logFormat = self::$format;

			try {
				throw new Exception();
			} catch(Exception $e) {
				$cb1 = $e->getTrace();
				$cb[0] = $cb1[3]['class'].'::'.$cb1[1]['function'];
				$cb[1] = $cb1[2]['file'];
				$cb[2] = $cb1[2]['line'];
			}
						
			$logFormat = str_replace('__LEVEL__', $level, $logFormat);
			$logFormat = str_replace('__FUNCTION__', $cb[0], $logFormat);
			$logFormat = str_replace('__FILE__', $cb[1], $logFormat);
			$logFormat = str_replace('__LINE__', $cb[2], $logFormat);
			$logFormat = str_replace('__DATE__', date(self::$cfg->date_format), $logFormat);
			$logFormat = str_replace('__TIME__', date(self::$cfg->time_format), $logFormat);
			$message = str_replace('__MESSAGE__', $message, $logFormat);
			
			return $message; 
		}
		
		public static function close() {
			if (self::$disabled) return;
			try {
				fclose(self::$target);
				self::$target = false;
			} catch (Exception $e) {} 
		}
	}
?>